//
//  WCOrderPicCell.m
//  订单确认
//
//  Created by wsj2015 on 15/12/25.
//  Copyright © 2015年 wsj2015. All rights reserved.
//

#import "WCOrderPicCell.h"
#import "WSJAnimationTool.h"
#import "WCOrderPicCell.h"
#import "Masonry.h"



@implementation GestureImageView

- (void)setTapG:(UITapGestureRecognizer *)tapG
{
    _tapG =tapG;
    [self addGestureRecognizer:tapG];
}
- (void)setLongG:(UILongPressGestureRecognizer *)longG
{
    _longG =longG;
    [self addGestureRecognizer:longG];
}
- (void)dealloc{

    
}

@end



@interface WCOrderPicCell () <UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIAlertViewDelegate>

@end

@implementation WCOrderPicCell
{
    UIView *_bgView;
    BOOL _isShaking;
    BOOL _isAbleDelect;
    UIImageView *_currentImage;
    NSMutableArray  *_temArrary;
   
   
}
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
//+ (WCOrderPicCell *)cellForTableView:(UITableView *)tableView
//{
//    
//    WCOrderPicCell *cell =[tableView dequeueReusableCellWithIdentifier:@"WCOrderPicCellId"];
//    if (cell == nil) {
//        cell = [self initWithStyle];
//    }
//    return cell;
//    
//}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self =[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.backgroundColor = kAppBg;
        CGFloat w = [UIScreen mainScreen].bounds.size.width;
        CGFloat h =[UIScreen mainScreen].bounds.size.width /3.422;
        UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(-8, 8, w+16, h)];
        bgView.layer.borderWidth = 1;
        bgView.backgroundColor =[UIColor whiteColor];
        bgView.layer.borderColor =kBorderColor.CGColor;
        _bgView = bgView;
        _bgView.userInteractionEnabled = YES;
        _isShaking = NO;
        _isAbleDelect =NO;
        [self.contentView addSubview:bgView];
        _temArrary  =[NSMutableArray new];
        
        [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(-8);
            make.top.equalTo(self.contentView.mas_top).offset(8);
            make.right.equalTo(self.contentView.mas_right).offset(8);
            make.height.mas_equalTo(h);
            make.bottom.equalTo(self.contentView.mas_bottom).offset(0);
            
        }];
        [self createImageViews];
        
    }


    return self;
}
- (void)createImageViews
{
    
    CGFloat w =([UIScreen mainScreen].bounds.size.width-32)/3;
    CGFloat h =[UIScreen mainScreen].bounds.size.width /3.422;
   
    for (int i =0; i<3; i++) {
        GestureImageView *imageView =[[GestureImageView alloc] initWithFrame:CGRectMake(16+(w+8)*i, 8,w, h-16)];
        imageView.image  =[UIImage imageNamed:@"图片占位"];
        imageView.tag = 2000+i;
        imageView.userInteractionEnabled = YES;
        
         imageView.tapG =[self createTapG];
          imageView.longG =[self createLongG];
       
        [imageView.longG removeTarget:self action:@selector(longAction:)];
        [_bgView addSubview:imageView];
        
        UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame =CGRectMake(w-25, 0, 25, 25);
        [btn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
//        btn.alpha = 0;
        btn.tag = 300;
        btn.alpha = 0;
        [btn setBackgroundImage:[UIImage imageNamed:@"dele_iocn"] forState:UIControlStateNormal];
        
//        [btn setBackgroundColor:[UIColor redColor]];
        [imageView addSubview:btn];
 \
        CGFloat left_make =16+(w+8)*i;
        if (i ==0) {
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(_bgView.mas_left).offset(left_make);
                make.top.equalTo(_bgView.mas_top).offset(8),
                make.width.mas_equalTo(w);
                make.height.mas_equalTo(h -16);
                make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
                
            }];
            
            
        }else if(i ==1){
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.left.equalTo(_bgView.mas_left).offset(left_make);
                make.top.equalTo(_bgView.mas_top).offset(8),
                make.width.mas_equalTo(w);
                   make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
            }];
        
        }else if(i ==2){
            [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.left.equalTo(_bgView.mas_left).offset(left_make);
                make.top.equalTo(_bgView.mas_top).offset(8),
                make.width.mas_equalTo(w);
                make.bottom.equalTo(_bgView.mas_bottom).offset(-8);
            }];
            
        }
        
       
        
        
    }
    
    
  

}

- (UILongPressGestureRecognizer *)createLongG
{
    UILongPressGestureRecognizer *longPress =[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longAction:)];
    // 设置长按手势需要几个手指同时按住 ，默认为1
    longPress.numberOfTouchesRequired = 1;
    
    // 设置长按手势最小的判定生效时间
    longPress.minimumPressDuration = 0.5;
    return longPress;
}
- (UITapGestureRecognizer *)createTapG
{
    return [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picImage:)];

}
- (void)picImage:(UITapGestureRecognizer *)tapG
{
    
    if (_isShaking) {
        return;
    }
    UIImageView *imageView =(UIImageView *)tapG.view;
    
    _currentImage =imageView;
    
    UIActionSheet *picSheet =[[UIActionSheet alloc]initWithTitle:@"选择照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册中获取",@"拍照", nil];
    
    [picSheet showInView:self.delegateController().view];
    
    
}
//选项表
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    
    switch (buttonIndex) {
        case 0:
            
            [self localPhoto];
            
            break;
        case 1:
            [self takePhoto];
            break;
            
        default:
            _currentImage =nil;
            break;
    }
}
// 照相操作
- (void)takePhoto
{
    UIImagePickerControllerSourceType type =UIImagePickerControllerSourceTypeCamera;
    if ([UIImagePickerController isSourceTypeAvailable:type]) {
        UIImagePickerController *picker =[[UIImagePickerController alloc]init];
        picker.delegate =self;
        picker.allowsEditing =YES;
        picker.sourceType =type;
        picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        if (self.delegateController) {
             [self.delegateController() presentViewController:picker animated:YES completion:nil];
        }
    }
    
}
//本地操作
- (void)localPhoto
{
    UIImagePickerControllerSourceType type =UIImagePickerControllerSourceTypePhotoLibrary;
    UIImagePickerController *picker =[[UIImagePickerController alloc]init];
    picker.sourceType =type;
    picker.delegate =self;
    [self.delegateController() presentViewController:picker animated:YES completion:nil];
    
    
    
}
#pragma -mark 图片 协议方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image =info[UIImagePickerControllerOriginalImage];
    
    [_temArrary addObject:image];
    if (_currentImage) {
        GestureImageView *imageView =(GestureImageView *)[_bgView viewWithTag:_currentImage.tag];
       
        [imageView.tapG removeTarget:self action:@selector(picImage:)];
        [imageView.longG addTarget:self action:@selector(longAction:)];
        _currentImage.image = image;
        
        NSLog(@"_currentImage=====%i",_currentImage.tag);
       
    }
    
    _currentImage = nil;
    
    [self.delegateController() dismissViewControllerAnimated:YES completion:nil];
    
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    
    
    [self.delegateController() dismissViewControllerAnimated:YES completion:nil];
    
}



- (void)deleteAction:(UIButton *)btn
{
    btn.alpha =0;
    GestureImageView *imageView =(GestureImageView *)btn.superview;
    [_temArrary removeObject:imageView.image];
    [self shakeAnimation:NO withView:imageView];
    [imageView setImage:[UIImage imageNamed:@"图片占位"]];
    [imageView.tapG addTarget:self action:@selector(picImage:)];
    [imageView.longG removeTarget:self action:@selector(longAction:)];
   
    
}

- (void)longAction:(UILongPressGestureRecognizer *)longG
{
    if (longG.state == UIGestureRecognizerStateEnded) {
        return;
    }
   UIImageView *tapView =(UIImageView *)longG.view;
    if (_isShaking ==NO) {
        [self shakeAnimation:YES withView:tapView];
    }else{
        [self shakeAnimation:NO withView:tapView];
    }
    

}

- (void)shakeAnimation:(BOOL)start withView:(UIView *)tapView
{
    CABasicAnimation *shakeAnimation =[WSJAnimationTool shakeAnimationWithView:tapView];
    UIButton *btn =(UIButton *)[tapView viewWithTag:300];
    if (start) {
        _isShaking = YES;
        
        btn.alpha =1;
        [WSJAnimationTool animationMoveUp:btn duration:0.34f];
    }else{
        [tapView.layer removeAllAnimations];
        _isShaking =NO;
        btn.alpha =0;
         [WSJAnimationTool animationMoveDown:btn duration:0.34f];
      
    }

}
- (NSArray *)imageArray
{
    return [_temArrary copy];
    

}
@end




